# -*- coding:utf-8 -*-
from fastapi import HTTPException
from sqlalchemy.exc import OperationalError
from sqlalchemy import update, delete, select, func
from core.curd import PageInfo


class IBaseDao:
    """
    通用类， 基础的增删改查操作
    """
    async def select_by_id(self, ids):
        async with self.db_session() as db:
            try:
                data = await self.base.get(db, pk=ids)
                return data
            except OperationalError as e:
                raise HTTPException(status_code=500, detail='数据库服务连接失败!')
            except Exception as e:
                raise HTTPException(status_code=500, detail=str(e))
            finally:
                await db.close()

    async def query(self, stmt):
        async with self.db_session() as db:
            try:
                ret = await db.execute(stmt)

                return ret.fetchall()
            except OperationalError as e:
                raise HTTPException(status_code=500, detail='数据库服务连接失败!')
            except Exception as e:
                raise HTTPException(status_code=500, detail=str(e))
            finally:
                await db.close()
                
    async def select_page(self, page: PageInfo, where: dict):
        async with self.db_session() as db:
            try:
                return await self.base.select_page(db, page, where)
            except OperationalError as e:
                raise HTTPException(status_code=500, detail='数据库服务连接失败!')
            except Exception as e:
                raise HTTPException(status_code=500, detail=str(e))
            finally:
                await db.close()

    async def update(self, entity):
        async with self.db_session() as db:
            try:
                r = await self.base.update(db, entity)
                await db.commit()
                return r
            except OperationalError as e:
                raise HTTPException(status_code=500, detail='数据库服务连接失败：'+str(e))
            except Exception as e:
                raise HTTPException(status_code=500, detail=str(e))
            finally:
                await db.close()

    async def insert(self, post, last_id: bool = False):
        async with self.db_session() as db:
            try:
                r = await self.base.create(db, post, last_id)
                await db.commit()
                return r
            except OperationalError as e:
                raise HTTPException(status_code=500, detail='数据库服务连接失败：'+str(e))
            except Exception as e:
                raise HTTPException(status_code=500, detail=str(e))
            finally:
                await db.close()

    async def delete_by_id(self, id):
        async with self.db_session() as db:
            try:
                rs = await self.base.delete_by_id(db, id)
                await db.commit()
            except OperationalError as e:
                raise HTTPException(status_code=500, detail='数据库服务连接失败!')
            except Exception as e:
                raise HTTPException(status_code=500, detail=str(e))
            finally:
                await db.close()

    async def delete_batch_ids(self, ids):
        async with self.db_session() as db:
            try:
                rs = await self.base.delete_by_ids(db, ids)
                await db.commit()
            except OperationalError as e:
                raise HTTPException(status_code=500, detail='数据库服务连接失败!')
            except Exception as e:
                raise HTTPException(status_code=500, detail=str(e))
            finally:
                await db.close()
